package com.pactera.madp.cp.controller.oms.wm;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.bo.CorpShowBO;
import com.pactera.madp.cp.api.bo.StepCreateBO;
import com.pactera.madp.cp.api.dto.ReqCorpStoreDTO;
import com.pactera.madp.cp.api.dto.ReqStepUpdateDTO;
import com.pactera.madp.cp.api.entity.wm.WorkMsgConfigEntity;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.service.IChatConfigService;
import com.pactera.madp.cp.service.ICorpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: yangpengwei
 * @time: 2020/12/2 11:05 上午
 * @description 会话内容存档配置
 */
@RestController
@AllArgsConstructor
@RequestMapping("/work-msg-config")
@Api(value = "work-msg-config", tags = "会话内容存档配置")
public class ChatConfigController {

    private IChatConfigService chatConfigService;
    private ICorpService corpService;

    /**
     * 会话内容存档列表
     * @param page
     * @return
     */
    @GetMapping(value = "/page")
    @ApiOperation(value = "会话内容存档列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="corpName",value="企业名称",dataType ="String")
    })
    public R getCorpList(Page page) {
        return corpService.getCorpPageList(page);
    }

    /**
     * 企业信息查看
     */
    @GetMapping
    @ApiOperation(value = "企业信息查看")
    public R<CorpShowBO> corpShow() {
        Integer corpId = CorpContextHolder.getCorpId();
        return R.ok(chatConfigService.getCorpShowInfo(corpId));
    }

    /**
     * 步骤页面-企业信息查询
     */
    @GetMapping("/step-info")
    @ApiOperation(value = "步骤页面-企业信息查询")
    public R<StepCreateBO> stepInfo() {
        return R.ok(chatConfigService.getStepInfo());
    }

    /**
     * 步骤页面-企业信息添加
     * @param dto
     * @return
     */
    @SysLog("步骤页面-企业信息添加")
    @PostMapping("/step-store")
    @ApiOperation(value = "步骤页面-企业信息添加")
    public R<Boolean> stepStore(@RequestBody ReqCorpStoreDTO dto) {
        return chatConfigService.setCorpStore(dto) ? R.ok() : R.failed();
    }

    /**
     * 步骤页面-添加客服提交资料
     * @return
     */
    @SysLog("步骤页面-添加客服提交资料")
    @PutMapping("/step-service/{id}")
    @ApiOperation(value = "步骤页面-添加客服提交资料")
    public R<Boolean> stepService(@PathVariable Integer id) {
        return chatConfigService.stepService(id) ? R.ok() : R.failed();
    }

    /**
     * 步骤页面-后台配置
     */
    @SysLog("步骤页面-后台配置")
    @PutMapping("/step-config")
    @ApiOperation(value = "步骤页面-后台配置")
    public R<Boolean> stepConfig(@RequestBody ReqStepUpdateDTO req) {
        return chatConfigService.stepConfig(req) ? R.ok() : R.failed();
    }

    /**
     * 设置会话存档开通状态
     */
    @SysLog("设置会话存档开通状态")
    @PutMapping("/chat-status")
    @ApiOperation(value = "设置会话存档开通状态")
    public R<Boolean> stepConfig() {
        List<WorkMsgConfigEntity> list = chatConfigService.list();
        if(list.isEmpty()) {
            return R.failed(Boolean.FALSE);
        }
        WorkMsgConfigEntity entity = list.get(0);
        if(entity.getChatApplyStatus() != 4) {
            return R.failed(Boolean.FALSE);
        }
        entity.setChatStatus(1);
        return chatConfigService.updateById(entity) ? R.ok(true) : R.failed(false);
    }

}
